package dic

import (
	"bytes"
	"fmt"
	"os/exec"
	"time"
)

var (
	done  chan bool
	cache []byte
	msg   Msg
)

type Msg struct {
	Num  int
	Done bool
}

func init() {
	cache = make([]byte, 0)
	done = make(chan bool)
}
func Listener(msgChan chan<- Msg) {
	for {
		time.Sleep(time.Second)
		cmd := exec.Command("xclip", "-o")
		out, err := cmd.Output()
		if err != nil {
			fmt.Println(err)
			msg.Done = true
			msgChan <- msg
			continue
		}
		fmt.Printf("%s\n", out)
		if len(out) > 0 {
			if !bytes.Equal(out, cache) {
				msg.Num = len(out)
				msgChan <- msg
				cache = out
				fmt.Println("go to GUI")
				go GUITranslate(string(out), done)
				fmt.Println("wait for done")
				<-done
				fmt.Println("after done")
			}
		}
	}
}
